using System.Collections.Generic;
using System.Collections.ObjectModel;
using CodePlex.DnD4eCampaignManager.Presentation;
using CodePlex.DnD4eCampaignManager.Presentation.ViewInterfaces;
using CodePlex.DnD4eCampaignManager.Repositories;
using CodePlex.DnD4eCampaignManager.Services.Interfaces;
using NUnit.Framework;
using Rhino.Mocks;
using StructureMap.AutoMocking;

namespace CodePlex.DnD4eCampaignManager.CampaignManagerTests.PresentationTests
{
    [TestFixture]
    public class CharacterManagerPresenterTests
    {
        private RhinoAutoMocker<CampaignManagerPresenter> _campaignManagerPresenterMocker;

        [Test]
        public void CloseSelectedTabDoesNothingWhenATabIsNotSelected()
        {
            _campaignManagerPresenterMocker.Get<ICampaignManagerView>().Stub(x => x.SelectedTabName).Return(string.Empty);
            _campaignManagerPresenterMocker.Get<ICampaignManagerView>().Stub(x => x.TabCount).Return(1);
            _campaignManagerPresenterMocker.Get<ICampaignManagerView>().Expect(x => x.CloseButtonVisible = true);

            _campaignManagerPresenterMocker.ClassUnderTest.CloseSelectedTab();
        }

        [Test]
        public void CloseSelectedTabRemovesATabWhenATabIsSelected()
        {
            _campaignManagerPresenterMocker.Get<ICampaignManagerView>().Stub(x => x.SelectedTabName).Return("Name");
            _campaignManagerPresenterMocker.Get<ICampaignManagerView>().Stub(x => x.RemoveTab("Name"));
            _campaignManagerPresenterMocker.Get<ICampaignManagerView>().Stub(x => x.TabCount).Return(0);
            _campaignManagerPresenterMocker.Get<ICampaignManagerView>().Expect(x => x.CloseButtonVisible = false);

            _campaignManagerPresenterMocker.ClassUnderTest.CloseSelectedTab();
        }

        [Test]
        public void DisplayCombatTrackerLoadsATabWithTheCombatTracker()
        {
            _campaignManagerPresenterMocker.Get<ICampaignManagerView>().Stub(x => x.AddCombatTrackerTab());

            _campaignManagerPresenterMocker.ClassUnderTest.DisplayCombatTracker();
        }

        [Test]
        public void DisplayMonstersLoadsAllMonstersFromTheRepositoryAndPushesThemToTheView()
        {
            ReadOnlyCollection<string> monsters = new ReadOnlyCollection<string>(new List<string>
                                                                                     {
                                                                                         "B",
                                                                                         "A",
                                                                                         "C"
                                                                                     });
            string[] sortedMonsters = new[] { "A", "B", "C" };
            _campaignManagerPresenterMocker.Get<IMonsterRepository>().Stub(x => x.GetAllNames()).Return(monsters);
            _campaignManagerPresenterMocker.Get<ICampaignManagerView>().Stub(x => x.MonsterNames = sortedMonsters);
            _campaignManagerPresenterMocker.ClassUnderTest.DisplayMonsters();
        }

        [Test]
        public void DisplayPlayerCharactersLoadsAllPlayerCharactersFromTheRepositoryAndPushesThemToTheView()
        {
            ReadOnlyCollection<string> playerCharacters = new List<string>
                                                              {
                                                                  "B",
                                                                  "A",
                                                                  "C"
                                                              }.AsReadOnly();
            string[] sortedPlayerCharacters = new[] { "A", "B", "C" };
            _campaignManagerPresenterMocker.Get<IPlayerCharacterRepository>().Stub(x => x.GetAllNames()).Return(playerCharacters);
            _campaignManagerPresenterMocker.Get<ICampaignManagerView>().Expect(x => x.PlayerCharacterNames = sortedPlayerCharacters);

            _campaignManagerPresenterMocker.ClassUnderTest.DisplayPlayerCharacters();
        }

        [SetUp]
        public void SetUp()
        {
            _campaignManagerPresenterMocker = new RhinoAutoMocker<CampaignManagerPresenter>();
        }

        [TearDown]
        public void TearDown()
        {
            _campaignManagerPresenterMocker.Get<ICampaignManagerView>().VerifyAllExpectations();
            _campaignManagerPresenterMocker.Get<IPlayerCharacterRepository>().VerifyAllExpectations();
            _campaignManagerPresenterMocker.Get<IMonsterRepository>().VerifyAllExpectations();
            _campaignManagerPresenterMocker.Get<IDungeonsAndDragonsInsiderMonsterImporter>().VerifyAllExpectations();
            _campaignManagerPresenterMocker.Get<IDungeonsAndDragonsInsiderPlayerCharacterImporter>().VerifyAllExpectations();
            _campaignManagerPresenterMocker.Get<ILocationRepository>().VerifyAllExpectations();
            _campaignManagerPresenterMocker.Get<IOpenFileView>().VerifyAllExpectations();
        }

        [Test]
        public void WhenAMonsterMenuItemIsClickedTheMonsterEditorTabIsLoadedWithThatMonster()
        {
            _campaignManagerPresenterMocker.Get<ICampaignManagerView>().Stub(x => x.ClickedMenuItemTag).Return("Monster_YourMom");
            _campaignManagerPresenterMocker.Get<ICampaignManagerView>().Stub(x => x.ClickedMenuItemText).Return("Your Mom");
            _campaignManagerPresenterMocker.Get<ICampaignManagerView>().Stub(x => x.TabCount).Return(1);
            _campaignManagerPresenterMocker.Get<ICampaignManagerView>().Expect(x => x.AddMonsterEditorTab("Your Mom"));
            _campaignManagerPresenterMocker.Get<ICampaignManagerView>().Expect(x => x.CloseButtonVisible = true);

            _campaignManagerPresenterMocker.ClassUnderTest.ProcessMenuItemClick();
        }

        [Test]
        public void WhenAPlayerCharacterMenuItemIsClickedThePlayerCharacterEditorTabIsLoadedWithThatPlayerCharacter()
        {
            _campaignManagerPresenterMocker.Get<ICampaignManagerView>().Stub(x => x.ClickedMenuItemTag).Return("PlayerCharacter_YourMom");
            _campaignManagerPresenterMocker.Get<ICampaignManagerView>().Stub(x => x.ClickedMenuItemText).Return("Your Mom");
            _campaignManagerPresenterMocker.Get<ICampaignManagerView>().Stub(x => x.TabCount).Return(1);
            _campaignManagerPresenterMocker.Get<ICampaignManagerView>().Expect(x => x.AddPlayerCharacterEditorTab("Your Mom"));
            _campaignManagerPresenterMocker.Get<ICampaignManagerView>().Expect(x => x.CloseButtonVisible = true);

            _campaignManagerPresenterMocker.ClassUnderTest.ProcessMenuItemClick();
        }

        [Test]
        public void WhenTheCombatTrackerMenuItemIsClickedTheCombatTrackerTabIsLoaded()
        {
            _campaignManagerPresenterMocker.Get<ICampaignManagerView>().Stub(x => x.ClickedMenuItemTag).Return("CombatTracker");
            _campaignManagerPresenterMocker.Get<ICampaignManagerView>().Stub(x => x.TabCount).Return(1);
            _campaignManagerPresenterMocker.Get<ICampaignManagerView>().Expect(x => x.AddCombatTrackerTab());
            _campaignManagerPresenterMocker.Get<ICampaignManagerView>().Expect(x => x.CloseButtonVisible = true);

            _campaignManagerPresenterMocker.ClassUnderTest.ProcessMenuItemClick();
        }

        [Test]
        public void WhenTheNewLocationMenuItemIsClickAnEmptyLocationEditorTabIsOpened()
        {
            _campaignManagerPresenterMocker.Get<ICampaignManagerView>().Stub(x => x.ClickedMenuItemTag).Return("NewLocation");
            _campaignManagerPresenterMocker.Get<ICampaignManagerView>().Stub(x => x.TabCount).Return(1);
            _campaignManagerPresenterMocker.Get<ICampaignManagerView>().Expect(x => x.AddLocationEditorTab(string.Empty));
            _campaignManagerPresenterMocker.Get<ICampaignManagerView>().Expect(x => x.CloseButtonVisible = true);

            _campaignManagerPresenterMocker.ClassUnderTest.ProcessMenuItemClick();
        }

        [Test]
        public void WhenTheNewMonsterMenuItemIsClickedTheMonsterEditorTabIsLoaded()
        {
            _campaignManagerPresenterMocker.Get<ICampaignManagerView>().Stub(x => x.ClickedMenuItemTag).Return("NewMonster");
            _campaignManagerPresenterMocker.Get<ICampaignManagerView>().Stub(x => x.TabCount).Return(1);
            _campaignManagerPresenterMocker.Get<ICampaignManagerView>().Expect(x => x.AddMonsterEditorTab(string.Empty));
            _campaignManagerPresenterMocker.Get<ICampaignManagerView>().Expect(x => x.CloseButtonVisible = true);

            _campaignManagerPresenterMocker.ClassUnderTest.ProcessMenuItemClick();
        }
    }
}